package com.tunnel.common.compile;

import java.io.File;
import java.util.Scanner;

import org.axe.util.StringUtil;

import com.ajida.util.CmdUtil;
import com.ajida.util.SSHConfig;
import com.ajida.util.SSHUtil;
import com.ajida.util.ZipUtil;

import ch.ethz.ssh2.Connection;

public class CompilerOnWindows {
	public static void main(String[] args) {
		try {
			File file = new File("");
			//base path
			String tunnel_common_root_path = file.getAbsolutePath();
			String disk_character = tunnel_common_root_path.split(":")[0]+":";
			
			
			
			//安装common包
			String result = CmdUtil.exec(new String[]{
				disk_character,
				"cd "+tunnel_common_root_path,
				"mvn clean install"
			});
			if(!result.contains("BUILD SUCCESS")){
				throw new Exception("tunnel-common mvn install failed");
			}
			
			//打包tunnel-client
			result = CmdUtil.exec(new String[]{
				disk_character,
				"cd "+tunnel_common_root_path,
				"cd ../tunnel-client",
				"mvn clean package"
			});
			if(!result.contains("BUILD SUCCESS")){
				throw new Exception("tunnel-client mvn package failed");
			}
			
			//打包tunnel-server
			result = CmdUtil.exec(new String[]{
				disk_character,
				"cd "+tunnel_common_root_path,
				"cd ../tunnel-server",
				"mvn clean package"
			});
			if(!result.contains("BUILD SUCCESS")){
				throw new Exception("tunnel-server mvn package failed");
			}
			
			//复制新的jar包
			try {
				CmdUtil.exec(new String[]{
					disk_character,
					"cd "+tunnel_common_root_path,
					"cd ../tunnel-client/tunnel-client/bin",
					"del /f /s /q tunnel-*.jar",
				});
			} catch (Exception e) {}
			CmdUtil.exec(new String[]{
				disk_character,
				"cd "+tunnel_common_root_path,
				"cd ../tunnel-common/target/",
				"copy \"tunnel-common-*.jar\" \""+tunnel_common_root_path+"/../tunnel-client/tunnel-client/bin\"",
				"cd "+tunnel_common_root_path,
				"cd ../tunnel-client/target/",
				"copy \"tunnel-client-*.jar\" \""+tunnel_common_root_path+"/../tunnel-client/tunnel-client/bin\""
			});
			
			try {
				CmdUtil.exec(new String[]{
					disk_character,
					"cd "+tunnel_common_root_path,
					"cd ../tunnel-server/tunnel-server/bin",
					"del /f /s /q tunnel-*.jar",
				});
			} catch (Exception e) {}
			CmdUtil.exec(new String[]{
				disk_character,
				"cd "+tunnel_common_root_path,
				"cd ../tunnel-common/target/",
				"copy \"tunnel-common-*.jar\" \""+tunnel_common_root_path+"/../tunnel-server/tunnel-server/bin\"",
				"cd "+tunnel_common_root_path,
				"cd ../tunnel-server/target/",
				"copy \"tunnel-server-*.jar\" \""+tunnel_common_root_path+"/../tunnel-server/tunnel-server/bin\""
			});
			
			//压缩zip
			try {
				CmdUtil.exec(new String[]{
					disk_character,
					"cd "+tunnel_common_root_path,
					"cd ../tunnel-client",
					"del /f /s /q tunnel-*.zip",
				});
			} catch (Exception e) {}
			ZipUtil.compressDir(new File(tunnel_common_root_path+"\\..\\tunnel-client\\tunnel-client"), tunnel_common_root_path+"\\..\\tunnel-client\\tunnel-client.zip");
			try {
				CmdUtil.exec(new String[]{
					disk_character,
					"cd "+tunnel_common_root_path,
					"cd ../tunnel-server",
					"del /f /s /q tunnel-*.zip",
				});
			} catch (Exception e) {}
			ZipUtil.compressDir(new File(tunnel_common_root_path+"\\..\\tunnel-server\\tunnel-server"), tunnel_common_root_path+"\\..\\tunnel-server\\tunnel-server.zip");
			
			//更新jar包到线上
			System.out.println("enter 114 password:");
			Scanner sc = new Scanner(System.in);
			String password = sc.nextLine();
			sc.close();
			
			
			SSHConfig sshConfig114 = new SSHConfig("218.90.120.43", "root", password);
			Connection connect114 = SSHUtil.connect(sshConfig114);
			String pid = SSHUtil.grepPid("tunnel",10, connect114);
			while(StringUtil.isNotEmpty(pid)){
				SSHUtil.exec(connect114,"kill -9 "+pid,10);
				pid = SSHUtil.grepPid("tunnel",10, connect114);
			}
			SSHUtil.exec(connect114, "rm -rf /usr/local/tunnel-server/bin/tunnel-*.jar", 10);
			SSHUtil.uploadFile(tunnel_common_root_path+"\\..\\tunnel-server\\tunnel-server\\bin\\tunnel-server-0.1.jar", "/usr/local/tunnel-server/bin", connect114);
			SSHUtil.uploadFile(tunnel_common_root_path+"\\..\\tunnel-server\\tunnel-server\\bin\\tunnel-common-0.1.jar", "/usr/local/tunnel-server/bin", connect114);
			SSHUtil.exec(connect114,new String[]{
				"cd /usr/local/tunnel-server",
				"./start.sh '/usr/local/tunnel-server/sys.properties'"
			},10,false);

			
/*
			SSHConfig sshConfig45 = new SSHConfig("192.168.199.45", "root", "ybsl1234");
			Connection connect45 = SSHUtil.connect(sshConfig45);
			pid = SSHUtil.grepPid("tunnel",10, connect45);
			while(StringUtil.isNotEmpty(pid)){
				SSHUtil.exec(connect45,"kill -9 "+pid,10);
				pid = SSHUtil.grepPid("tunnel",10, connect45);
			}
			SSHUtil.exec(connect45, "rm -rf /usr/local/tunnel-client/bin/tunnel-*.jar", 10);
			SSHUtil.uploadFile(tunnel_common_root_path+"\\..\\tunnel-client\\tunnel-client\\bin\\tunnel-client-0.1.jar", "/usr/local/tunnel-client/bin", connect45);
			SSHUtil.uploadFile(tunnel_common_root_path+"\\..\\tunnel-client\\tunnel-client\\bin\\tunnel-common-0.1.jar", "/usr/local/tunnel-client/bin", connect45);
			SSHUtil.exec(connect45,new String[]{
					"cd /usr/local/tunnel-client",
					"./start.sh '/usr/local/tunnel-client/sys.properties'"
				},10,false);*/
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
